package com.ruoyi.tracker.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.tracker.domain.VehiclePosition;
import com.ruoyi.tracker.mapper.VehiclePositionMapper;
import com.ruoyi.tracker.service.IVehiclePositionService;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.domain.geo.Metrics;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author: 凯不忒埔
 * @ProjectName RuoYi-month
 * @ClassName IVehiclePositionServiceImpl
 * @Date: 2025/9/6 09:18
 * @Description:
 */
@Service
public class IVehiclePositionServiceImpl implements IVehiclePositionService {
    @Autowired
    private StringRedisTemplate redisTemplate;
//    @Autowired
//    private InfluxDB influxDB;
//    @Autowired
//    private VehiclePositionMapper vehicleInfoMapper;
    private static final String INFLUXDB_MEASUREMENT = "vehicle_position";
    private static final String REDIS_GEO_KEY = "vehicle:position:geo";

    @Override
    public boolean reportVehiclePosition(VehiclePosition vehiclePosition) {
        return false;
    }
    @Override
    public R<?> queryVehiclesWithin(Double longitude, Double latitude) {
        String key = "vehicles:geo";
        Distance distance = new Distance(5, Metrics.KILOMETERS);
        Circle within = new Circle(new org.springframework.data.geo.Point(longitude, latitude), distance);
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo()
                .radius(key, within);

        List<String> vehicleIds = results.getContent().stream()
                .map(geo -> geo.getContent().getName())
                .collect(Collectors.toList());

        return R.ok(vehicleIds);
    }
}